package cn.com.HiSting1990.MyList;

import java.util.Comparator;
import java.util.TreeMap;

public class myTreeMap {

    public static void main(String[] args) {

        //TreeMap是排序的Map
        TreeMap<Integer, String> treeMap1 = new TreeMap<>();
        myPut1(treeMap1);
        TreeMap<String, String> treeMap2 = new TreeMap<>();
        myPut2(treeMap2);
        System.out.println(111);
//        treeMap1.get("11") != null;

        //自定义排序的TreeMap
        TreeMap<String, String> treeMap3 = new TreeMap<String,String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //这里我们可以根据自己的需求写一个排序的逻辑
                //我们这里还没实现，后面我会实现
                //
                Integer i2 = Integer.valueOf(o2);
                Integer i1 = Integer.valueOf(o1);
                return o2.compareTo(o1);
            }
        });
        myPut3(treeMap3);
    }

    //我们把Key设置为String类型，可以看到它进行了排序
    private static TreeMap<String,String> myPut3(TreeMap<String,String> treeMap){
        treeMap.put("3","A");
        treeMap.put("5","B");
        treeMap.put("1","B");
        treeMap.put("23","B");
        treeMap.put("0","B");
        treeMap.put("2","B");
        return treeMap;
    }

    //我们把Key设置为Integer类型，可以看到它进行了排序
    private static TreeMap<Integer,String> myPut1(TreeMap<Integer,String> treeMap){
        treeMap.put(4,"A");
        treeMap.put(3,"B");
        treeMap.put(1,"B");
        treeMap.put(23,"B");
        treeMap.put(0,"B");
        treeMap.put(2,"B");
        return treeMap;
    }

    //我们把Key设置为String类型，也可以看到它对字符进行了排序
    //它们会对字符串的首字母进行排序，然后如果有相同的继续排第二字符
    //进行排序的Map建议使用这个Map
    private static TreeMap<String,String> myPut2(TreeMap<String,String> treeMap){
        treeMap.put("BE","B");
        treeMap.put("BC","B");
        treeMap.put("BD","F");
        treeMap.put("F","F");
        treeMap.put("C","C");
        treeMap.put("E","E");
        treeMap.put("D","D");
        treeMap.put("A","A");
        return treeMap;
    }
}
